package bosc.cn.service.wallet.impl;

import bosc.cn.constant.OrderConstants;
import bosc.cn.constant.WalletConstants;
import bosc.cn.dto.common.CommonResponse;
import bosc.cn.dto.common.ResponseWrapper;
import bosc.cn.service.wallet.WalletDetailService;
import bosc.cn.service.wallet.bo.WalletDetailReqBO;
import bosc.cn.service.wallet.bo.WalletDetailResBO;
import bosc.cn.service.wallet.bo.WalletRegisterResBO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mapper.WalletInfoMapper;
import model.WalletInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class WalletDetailServiceImpl implements WalletDetailService {

    @Autowired
    private WalletInfoMapper walletInfoMapper;

    @Override
    public ResponseWrapper<WalletDetailResBO> getWalletDetail(WalletDetailReqBO reqBO) {

        ResponseWrapper<WalletDetailResBO> resBO = new ResponseWrapper<>();
        CommonResponse head = new CommonResponse();
        WalletDetailResBO body = new WalletDetailResBO();

        try {

            // 正常通信
            head.setRetCd("000000");
            head.setRetMsg("通信成功");
            head.setRetTm(LocalDateTime.now());

            WalletInfo walletInfo = walletInfoMapper.selectOne(
                    new QueryWrapper<WalletInfo>().eq("wlt_id", reqBO.getWltId())
            );

            // 判空处理
            if (walletInfo == null) {
                body.setPrcSts("PR99");
                body.setPrcMsg("钱包不存在");
                resBO.setHead(head);
                resBO.setBody(body);
                return resBO;
            }

            // 判断钱包状态
            if (!WalletConstants.Status.WS01.equals(walletInfo.getWltSts())) {
                body.setPrcSts("PR88");
                body.setPrcMsg("钱包已注销");
                resBO.setHead(head);
                resBO.setBody(body);
                return resBO;
            }

            // 封装响应
            body.setPrcSts("PR00");
            body.setPrcMsg("钱包详细信息查询成功");
            body.setWltId(walletInfo.getWltId());
            body.setWltName(walletInfo.getWltName());
            body.setWltLvl(walletInfo.getWltLvl());
            body.setWltType(walletInfo.getWltType());
            body.setWltBnk(walletInfo.getWltBnk());
            body.setWltSts(walletInfo.getWltSts());
            body.setWltAuthDt(walletInfo.getWltAuthDt());
            body.setCstNm(walletInfo.getCstNm());
            body.setCstIdentTp(walletInfo.getCstIdentTp());
            body.setCstNo(walletInfo.getCstNo());
            body.setWltBal(walletInfo.getWltBal());
            body.setWltBalLmt(walletInfo.getWltBalLmt());
            body.setSngLmtAmt(walletInfo.getSngLmtAmt());
            body.setDlyLmtAmt(walletInfo.getDlyLmtAmt());
            body.setYrLmtAmt(walletInfo.getYrLmtAmt());

        } catch (Exception e) {
            // 出现异常，通信失败
            head.setRetCd("999999");
            head.setRetMsg("系统异常：" + e.getMessage());
            head.setRetTm(LocalDateTime.now());
        }
        resBO.setHead(head);
        resBO.setBody(body);
        return resBO;
    }
}
